/*
 * Home.java
 *
 * Created on January 29, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ge.info.pages;

import ge.info.domain.*;

import ge.ktapestry.pages.BasePageExt;
import org.apache.hivemind.util.PropertyUtils;
import org.apache.tapestry.IPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import java.util.List;
import org.apache.tapestry.annotations.InjectState;

public abstract class ServicePage extends BasePageExt 
{

    public abstract ServiceDetail getServiceDetail();

    public abstract void setServiceDetail(ServiceDetail serviceDetail);

    @InjectState("service")
    public abstract Service getService();

    public abstract void setService(Service service);



    public void submit(IRequestCycle cycle)
    {
        if (getDelegate().getHasErrors())
        {
            return;
        }
        boolean searchDetails = false;
        List<Integer> results;
        List<ServiceDetail> details = getService().getServiceDetails();
        for (ServiceDetail detail : details)
        {
            if (detail.getCriteria() != null || detail.getDateCriteria() != null)
            {
                searchDetails = true;
                break;
            }
        }

        results = getDbExecutor().searchDetails(getService(), getSession(), searchDetails);

        if (results.size() > 999)
        {
            results = results.subList(0, 999);
        }
        List<MainResult> mainResults = getDbExecutor().getMainResults(results);
        if (mainResults.size() < 1)
        {
            this.addWarning(new Exception("NO_DATA_FOUND"));
            return;
        }

        if (mainResults.size() == 1)
        {
            getDbExecutor().logMainResult(mainResults.get(0).getMainID(), getSession(), getService());

        }

        IPage page = cycle.getPage("MainResults");
        PropertyUtils.write(page, "mainResults", mainResults);
        PropertyUtils.write(page, "service", getService());
        cycle.activate(page);
    }
}
